﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqProductCategory
    {


        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);
        public List<ProductCategory> GetProductCategories(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<ProductCategoryEntity> query = db.ProductCategoryEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "ProductCategoryCode":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.ProductCategoryCode);
                            else
                                query = query.OrderByDescending(c => c.ProductCategoryCode);
                            break;

                    }
                }
                //var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
                return query.Select(c => Mapper.ToProductCategoryObject(c)).ToList();
            }
        }

        public ProductCategory GetProductCategory(int prodCatCode)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                return Mapper.ToProductCategoryObject(db.ProductCategoryEntities
                            .SingleOrDefault(p => p.ProductCategoryCode == prodCatCode));

            }
        }



    }
}
